Skip to content

[v24.x] backport ERR_REQUIRE_ASYNC_MODULE improvements - #65125

Open
joyeecheung wants to merge 2 commits into
nodejs:v24.x-stagingfrom
joyeecheung:backport-tla-24
Open

[v24.x] backport ERR_REQUIRE_ASYNC_MODULE improvements#65125
joyeecheung wants to merge 2 commits into
nodejs:v24.x-stagingfrom
joyeecheung:backport-tla-24

Conversation

@joyeecheung

Copy link
Copy Markdown
Member

This backports #64260 and #64154

Previously in order to collect the locations of the TLA, we wait
until right before evalutation to ensure instantiation is
completed so that we can use
v8::Module::GetStalledTopLevelAwaitMessages(). Now we try to
add an additioanl shortcut to the source code in the module wraps
instead during compilation for modules that contain TLAs and
use acron to locate the TLAs when we need to throw
ERR_REQUIRE_AYNSC_MODULE, so we can do this as early as before
instantiation and do not need to run the module again to collect
the locations.

In addition, we now collect the require stack for
ERR_REQUIRE_ASYNC_MODULE too for better metadata in the errors.

Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: nodejs#64154
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This brings back several improvements that were reverted by mistake when
landing https://redirect.github.com/nodejs/node/pull/64154

- Update the documentation about how the removal of side effects of
  source collection
- Add non-enumerable `requireStack` and `topLevelAwaitLocations`
  properties to `ERR_REQUIRE_ASYNC_MODULE`, the latter is only
  populated when --experimental-print-required-tla is enabled
- Add "Required module: <url>" to the error message to identify the
  required ESM entry point regardless of whether the flag is enabled
- Fix TLA caret column from 0-based to 1-based
- Store module source via a private symbol instead of a public property
- Use `hasAsyncGraph` (post-instantiation) in `throwIfAsyncGraph`
  instead of walking the graph before instantiation
- Merge the require stack checking into the
  `common.expectRequiredTLAError` helper.
- Removed tests that are made redundant by the snapshot tests

Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: nodejs#64260
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/vm

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. v24.x Issues that can be reproduced on v24.x or PRs targeting the v24.x-staging branch. labels Aug 7, 2026
Comment thread doc/api/errors.md
description: Added the `requireStack` and `topLevelAwaitLocations` properties.
-->

When trying to `require()` a [ES Module][], the module turns out to be asynchronous.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When trying to `require()` a [ES Module][], the module turns out to be asynchronous.
When trying to `require()` an [ES Module][], the module turns out to be asynchronous.

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.55357% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.31%. Comparing base (b881658) to head (54728e7).
⚠️ Report is 805 commits behind head on v24.x-staging.

Files with missing lines Patch % Lines
src/module_wrap.cc 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                Coverage Diff                @@
##           v24.x-staging   #65125      +/-   ##
=================================================
+ Coverage          89.92%   90.31%   +0.39%     
=================================================
  Files                686      711      +25     
  Lines             208389   228424   +20035     
  Branches           40077    43158    +3081     
=================================================
+ Hits              187387   206310   +18923     
- Misses             13238    14080     +842     
- Partials            7764     8034     +270     
Files with missing lines Coverage Δ
lib/internal/errors.js 97.68% <100.00%> (+0.05%) ⬆️
lib/internal/modules/cjs/loader.js 98.13% <100.00%> (-0.12%) ⬇️
lib/internal/modules/esm/loader.js 99.69% <100.00%> (+0.01%) ⬆️
lib/internal/modules/esm/module_job.js 99.26% <100.00%> (+2.86%) ⬆️
lib/internal/modules/esm/translators.js 97.54% <100.00%> (-0.14%) ⬇️
lib/internal/modules/esm/utils.js 100.00% <100.00%> (ø)
lib/internal/modules/helpers.js 98.28% <100.00%> (+0.05%) ⬆️
src/node_errors.h 86.95% <ø> (+0.47%) ⬆️
src/module_wrap.cc 75.64% <0.00%> (-0.22%) ⬇️

... and 296 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. v24.x Issues that can be reproduced on v24.x or PRs targeting the v24.x-staging branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants